<?php
/**
 * 《我的工资》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../finance/Salary.class.php');

class SalaryMy extends Salary {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));

		$controller->display($request, 'finance/salary/my/find');
		return true;
	}
	
	function _my() {		
		$emp = $_SESSION['id'];
		
		return sprintf(" emp = '%s'", $emp);
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'finance/salary/my/list');
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		
		if ($_SESSION['id'] == $row['emp']) {
			$controller->assign_by_ref('result', $row);
		}

		$controller->display($request, 'finance/salary/view');
		return true;
	}
	
	function auth($controller, $request) {
		
		if ($_SESSION['password_3']) {
			$data = array('status'=>'1');
		} else {
			$data = array('status'=>'2');
		}
		
		echo json_encode($data);
	}
	
	function login($controller, $request) {		
		$password = $request['password'];
		
		$emp = $_SESSION['id'];
		$s = $this->password($emp, '3');
		debug($s);
		debug(print_r($request, true));
		
		if (!empty($password) && $password == $s) {
			$data = array('status'=>'1');
			$_SESSION['password_3'] = true;
		} else {
			$data = array('status'=>'2');
		}
		
		echo json_encode($data);
	}
	
	function password($emp, $type) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND type = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_password, $emp, $type);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row['password'];
		}
		
		return '';	
	}
}
?>